

//
//  UiViewExtension.swift
//  YouCaiShoppingMall
//
//  Created by 优裁科技 on 2017/9/21.
//  Copyright © 2017年 优裁科技. All rights reserved.
//

import UIKit

/**
 * UIView 扩展
 */
extension UIView {

    //MARK: - 设置带文本的角标
    /**
     * 设置带文本的角标
     */
    func makeBadgeText(text:String,
                       textColor tColor:UIColor = UIColor.white,
                       txtFont tFont:UIFont = UIFont.systemFont(ofSize: 6),
                       backColor bColor:UIColor = UIColor.init().colorFromHexInt(hex: 0xFD3935)){

        var rect:CGRect?
        let textSize:CGSize = NSString.init(string: text).boundingRect(with: CGSize.init(width: self.frame.size.width, height: CGFloat.greatestFiniteMagnitude),
                                                                                  options: .usesLineFragmentOrigin,
                                                                                  attributes: [NSAttributedString.Key.font:tFont],
                                                                                  context: nil).size

        //UIButton
        if self.isKind(of: UIButton.classForCoder()) {
            unowned let weakButton:UIButton = self as! UIButton
            rect = CGRect.init(x: (weakButton.imageView?.frame.size.width)! * 0.5 + (weakButton.imageView?.frame.origin.x)!,
                               y: (weakButton.imageView?.frame.origin.y)!,
                               width: textSize.width + 8,
                               height: textSize.height)
        }
        else{
            rect = CGRect.init(x: self.frame.size.width - (textSize.width + 8) * 0.5,
                               y: -textSize.height * 0.5,
                               width: textSize.width + 8,
                               height: textSize.height)
        }

        let _txt:String = Int.init(text)! > 99 ? "…" : text
        self.badgeLable().makeBrdgeViewWithText(text: _txt, textColor: tColor, backColor: bColor, textFont: tFont, tframe: rect!)
    }

    //MARK: - 设置没有文本的角标
    /**
     * 设置没有文本的角标
     */
    func makeBadge(corner:CGFloat? = 3,cornerColor color:UIColor? = UIColor.init().colorFromHexInt(hex: 0xfd7e18)){

        let labBadge:YeeBadgeLable = self.badgeLable()
        labBadge.frame = CGRect.init(x: self.frame.size.width - corner!, y: -0.3 * corner!, width: corner! * 2, height: corner! * 2)
        labBadge.makeBrdgeViewWithCor(corner: corner!, CornerColor: color!)
    }

    //MARK: - 移除角标
    /**
     * 移除角标
     */
    func removeBadgeView() {
         self.badgeLable().removeFromSuperview()
    }

    /////////////////////////////////////////////////////////

    /** badgeLable  */
    private func badgeLable() -> YeeBadgeLable {

        var badgeLable:YeeBadgeLable? = self.viewWithTag(99999) as? YeeBadgeLable
        if badgeLable == nil {
            badgeLable = YeeBadgeLable.init()
            badgeLable?.tag = 99999

            self.addSubview(badgeLable!)
        }
        return badgeLable!
    }
    
    
   //MARK: - 设置圆角
   /// 设置视图圆角
   /// - Parameter corners: 圆角的方位(UIRectCorner.topRight,UIRectCorner.topLeft,...)
   /// - Parameter w: 边框宽度
   /// - Parameter bc: 变宽颜色
   /// - Parameter fc: 填充颜色
   /// - Parameter r: 圆角半径
   func setCornerRadiusFor(UIRectCorner corners:UIRectCorner,
                           borderWidth w:CGFloat,
                           borderColor bc:UIColor = UIColor.clear,
                           fillColor fc:UIColor = UIColor.clear,
                           andCorneradius r:CGFloat) -> CAShapeLayer {
       
           
       let path = UIBezierPath.init(roundedRect: self.bounds,
                                        byRoundingCorners: corners,
                                        cornerRadii: CGSize.init(width: r, height: r))
       
       let layer = CAShapeLayer.init()
       layer.path = path.cgPath
   
       layer.lineWidth = w
       layer.lineCap = CAShapeLayerLineCap.square
       layer.strokeColor = bc.cgColor
   
       //  注意直接填充layer的颜色，不需要设置控件view的backgroundColor
       layer.fillColor = fc.cgColor
       return layer
   }
}
